<template>
  <div style="display: inline-block;text-align: left;">
    <!-- 修改 - btn -->
    <el-button type="primary" @click="dialogTableVisible = true">调用</el-button>
    <el-dialog v-el-drag-dialog :visible.sync="dialogTableVisible" width="900px" title="调用" @close="close">
      <el-form ref="row_data" :model="row_data" label-position="left" label-width="100px" style="width: 750px; margin-left:50px;">
        <el-form-item :label="'ID'" prop="id">
          <el-input v-model="row_data.id" :disabled="true" placeholder="接口ID" />
        </el-form-item>
        <el-form-item :label="'接口标题'" prop="title">
          <el-input v-model="row_data.title" :disabled="true" placeholder="接口标题"/>
        </el-form-item>
        <el-form-item :label="'接口路径'" prop="path">
          <el-input v-model="row_data.path" :disabled="true" placeholder="接口路径"/>
        </el-form-item>
        <!--<el-form-item :label="'需输入的数据'" prop="needData">-->
        <!--&lt;!&ndash;<el-input v-model="row_data.needData" placeholder="需输入的数据"/>&ndash;&gt;-->
        <!--<el-input v-model="newNeedData" type="text"/>-->
        <!--</el-form-item>-->
        <el-form-item :label="'操作类型'" prop="action">
          <el-input v-model="row_data.action" :disabled="true" placeholder="操作类型"/>
        </el-form-item>
        <div v-for="(item,index) in newNeedData" :key="index" style="display: flex;flex-direction: column;">
          <div style="display: flex;flex-direction: row">
            <div style="width: 100px;margin: 15px 0px;display:table-cell">
              <span class="span">{{ item }}</span>
            </div>
            <el-input v-model="nNeedData[index]" placeholder="请输入的数据（‘？’为非必填项）" style="width: 50% !important;"/>
          </div>
        </div>
        <el-form-item>
          <el-button type="primary" @click="submitForm()">确定</el-button>
          <el-button @click="close()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import elDragDialog from '@/directive/el-dragDialog' // base on element-ui
import store from '@/store'
import { menuAction } from '@/api/wechat/menu'

export default {
  name: 'DragDialogDemo',
  directives: { elDragDialog },
  props: {
    row_data: {
      type: Object,
      default: function() {
        return null
      }
    }
  },
  data() {
    return {
      newNeedData: [],
      nNeedData: [],
      inputData: {},
      dialogTableVisible: false
    }
  },
  created: function() {
    console.log(this.row_data)
    this.needData()
  },
  methods: {
    needData() {
      this.newNeedData = this.row_data.needData.split(',')
      console.log('newNeedData', this.newNeedData)
    },
    submitForm() {
      this.newNeedData.forEach((item, index) => {
        var str = item.replace(/[\?]/g, '')// 去掉问号
        this.inputData[str] = this.nNeedData[index]
      })
      console.log('item', this.inputData)
      this.$refs.row_data.validate(valid => {
        if (valid) {
          const data = this.row_data
          console.log('data', data)
          console.log('text', this.inputData)
          menuAction(store.getters.token, { data: data, inputData: this.inputData }).then(res => {
            if (res.data.status) {
              this.$message.success('调用成功！')
              this.close()
            } else {
              this.$message.error('调用失败！原因：' + res.data.msg)
            }
          })
        } else {
          return false
        }
      })
    },
    close() {
      this.$emit('getLists')
      this.dialogTableVisible = false
    }
  }
}
</script>

<style>
  .span {
    weight: 80px;
    font-weight: bold;
  }
</style>
